/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2023-12-10
 * Time: 16:06
 */
public class BinaryTree {
    //定义树的结点类
    static class TreeNode {  //定义好三个属性
        public char val;
        public TreeNode left;  //左节点
        public TreeNode right;  //右节点

        public TreeNode(char val) {  //提供一个结点的构造方法  new一个新结点的时候是不知道左右子树的,所以不用构造
            this.val = val;
        }
    }

    //以穷举的方式创建一棵二叉树出来
    public TreeNode creatTree() {  //要返回树的根节点,所以返回类型是 TreeNode
        TreeNode A = new TreeNode('A');  //创建一个结点赋值
        TreeNode B = new TreeNode('B');  //创建一个结点赋值
        TreeNode C = new TreeNode('C');  //创建一个结点赋值
        TreeNode D = new TreeNode('D');  //创建一个结点赋值
        TreeNode E = new TreeNode('E');  //创建一个结点赋值
        TreeNode F = new TreeNode('F');  //创建一个结点赋值
        TreeNode G = new TreeNode('G');  //创建一个结点赋值
        TreeNode H = new TreeNode('H');  //创建一个结点赋值

        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;

        E.right = H;

        return A;  //返回根节点

    }

    // 前序遍历
    void preOrder(TreeNode root) {
        if (root == null) {
            return;  //空树是不需要遍历的
        }
        System.out.print(root.val + " ");
        preOrder(root.left);  //递归
        preOrder(root.right);

    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);

    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if (root == null) {
            if (root == null) {
                return;
            }
            postOrder(root.left);
            postOrder(root.right);
            System.out.print(root.val + " ");
        }

    }
}
